<script setup>
import AppPage from "@/pages/admin/layouts/AppPage.vue";
import {router} from "@inertiajs/vue3";
import useTable from "@/composables/useTable.js";
import {Message} from "@arco-design/web-vue";

const props = defineProps({
    data: {
        type: Array,
        default: () => []
    },
    pagination: {
        type: Object,
        default: () => ({
            current: 1,
            pageSize: 10,
            total: 0
        })
    },
    filters: {
        type: Object,
        default: () => ({
            keyword: '',
            startDate: '',
            endDate: '',
        })
    },
    title: {
        type: String,
        default: () => '登录日志'
    }
})

const {TableRef, bordered, scroll} = useTable()
const loading = ref(false);
const params = ref({
    keyword: '',
    startDate: '',
    endDate: '',
    page: 1,
})

const handleReset = () => {
    params.value = {
        keyword: '',
        startDate: '',
        endDate: '',
        page: 1,
    }
    handleSearch()
}

const handleSearch = () => {
    const filteredParams = Object.fromEntries(
        Object.entries(params.value).filter(([_, value]) =>
            value !== '' && value !== null && value !== undefined
        )
    );
    loading.value = true;
    try {
        router.get(route('admin.technical.logins.index'), filteredParams, {
                replace: true,
                preserveState: true // 保留其他状态
            }
        )
    } finally {
        loading.value = false;
    }
}

const handleQuickSearch = () => {
    if (!params.value.keyword.trim()) {
        Message.warning('请输入搜索关键词');
        return;
    }
    handleSearch()
}

const handlePageChange = (page) => {
    params.value.page = page
    handleSearch()
}

onMounted(() => {
    // 初始化参数
    params.value = {
        ...params.value,
        ...props.filters
    }
})
</script>

<template>
    <app-page :title="title">
        <a-card
            :body-style="{ padding: '0' }"
            :header-style="{ padding: '16px 20px', borderBottom: '1px solid #f0f0f0' }"
            :style="{ borderRadius: '8px', overflow: 'hidden' }"
            :title="title"
            class="custom-card shadow-sm transition-all duration-300 hover:shadow-md"
        >

            <!-- 顶部搜索栏 -->
            <div class="p-4 border-b border-gray-100 bg-gray-50">
                <div class="flex flex-wrap items-end gap-4">
                    <!-- 关键词搜索 -->
                    <div class="flex-1 min-w-[180px]">
                        <a-input
                            v-model="params.keyword"
                            allow-clear
                            placeholder="搜索账号、设备、浏览器、操作系统、IP"
                            @clear="handleSearch"
                            @press-enter="handleQuickSearch"
                        >
                            <template #prefix>
                                <icon-search/>
                            </template>
                        </a-input>
                    </div>

                    <div class="w-[360px]">
                        <RangeDate
                            v-model:end="params.endDate"
                            v-model:start="params.startDate"
                        />
                    </div>

                    <!-- 操作按钮 -->
                    <div class="flex gap-2">
                        <a-button type="primary" @click="handleSearch">
                            <template #icon>
                                <icon-search/>
                            </template>
                            搜索
                        </a-button>

                        <a-button @click="handleReset">
                            重置
                        </a-button>
                    </div>
                </div>
            </div>

            <div class="p-4">
                <a-table
                    ref="TableRef"
                    :bordered
                    :data="data"
                    :pagination
                    :scroll
                    :scrollbar="false"
                    row-key="id"
                    @page-change="handlePageChange"
                >
                    <template #columns>
                        <a-table-column :width="80" data-index="id" title="ID"/>
                        <a-table-column :min-width="120" data-index="name" title="账号"/>
                        <a-table-column :width="120" data-index="device" title="设备"/>
                        <a-table-column :width="120" data-index="browser" title="浏览器"/>
                        <a-table-column :width="120" data-index="os" title="操作系统"/>
                        <a-table-column :width="120" data-index="status" title="状态">
                            <template #cell="{ record }">
                                <a-badge :color="record.status ? '#165DFF' : '#F53F3F'"
                                         :text="record.status ? '成功' : '失败'"/>
                            </template>
                        </a-table-column>
                        <a-table-column :width="120" data-index="message" title="描述"/>
                        <a-table-column :width="140" data-index="login_ip" title="IP"/>
                        <a-table-column :width="180" data-index="login_at" title="时间"/>
                    </template>
                </a-table>

            </div>
        </a-card>
    </app-page>
</template>

<style scoped>

</style>
